﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.CarService.Illegals;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.CarService.Illegals
{
    public class IllegalRepository : DapperDbContextRepository, IIllegalRepository
    {
        public IllegalRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示违章记录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="time"></param>
        /// <param name="cardnumber"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<IllegalDto>> GetShowIllegal(string name, DateTime? time, string cardnumber, int pageindex, int pagesize)
        {
            PageResponse<IllegalDto> response = new PageResponse<IllegalDto>();
            var sql = $"select a.Id,a.IllegalNumber,a.Title,a.CarNumber,a.IllegalTime,a.CreateTime,a.Remark,a.IsDelete,a.IllegalContent,a.Result,b.DriverName as IllegalManager,b.License from Illegal a join Car b on a.CarNumber=b.CarNumber where a.IsDelete=0 ";

            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and a.Title like '%" + name + "%'";
            }
            if (time != null)
            {
                sql += $" and a.IllegalTime='{time}'";
            }
            if (!string.IsNullOrWhiteSpace(cardnumber))
            {
                sql += $" and a.CarNumber='{cardnumber}'";
            }
            var list = await this.QueryAsync<IllegalDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<IllegalDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除违章记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelIllegal(int id)
        {
            var sql = $"update Illegal set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除违章记录
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsIllegal(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Illegal set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="illegalDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateIllegal(IllegalDto illegalDto)
        {
            var sql = $"update Illegal set Title='{illegalDto.Title}',CarNumber='{illegalDto.CarNumber}',Remark='{illegalDto.Remark}',IllegalTime='{illegalDto.IllegalTime.ToString("yyyy/MM/dd")}',IllegalContent='{illegalDto.IllegalContent}',Result='{illegalDto.Result}' where Id='{illegalDto.Id}'";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加违章记录
        /// </summary>
        /// <param name="illegalDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddIllegal(IllegalDto illegalDto)
        {
            var sql = $"insert into Illegal values('{illegalDto.IllegalNumber}','{illegalDto.Title}','{illegalDto.CarNumber}','','{illegalDto.IllegalTime.ToString("yyyy/MM/dd")}','{illegalDto.Remark}',GETDATE(),0,'{illegalDto.IllegalContent}','{illegalDto.Result}')";
            var list = await ExecuteAsync
               (sql, illegalDto);
            return list;
        }
    }
}
